package com.wonders.framework.web.context;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * request上下文
 * 
 * @author WuBin
 * 
 */
public class RequestContext {

	private HttpSession session;

	private HttpServletRequest request;

	private HttpServletResponse response;

	private Map<String, Cookie> cookies;

	private final static ThreadLocal<RequestContext> contexts = new ThreadLocal<RequestContext>();

	/**
	 * 初始化请求上下文
	 * 
	 * @param req
	 * @param res
	 */
	public static RequestContext begin(HttpServletRequest req,
			HttpServletResponse res) {
		RequestContext rc = new RequestContext();
		rc.request = req;
		rc.response = res;
		rc.session = req.getSession();
		rc.cookies = new HashMap<String, Cookie>();
		Cookie[] cookies = req.getCookies();
		if (cookies != null)
			for (Cookie ck : cookies) {
				rc.cookies.put(ck.getName(), ck);
			}
		contexts.set(rc);
		return rc;
	}

	/**
	 * 获取当前请求上下文
	 * 
	 * @return
	 */
	public static RequestContext get() {
		return contexts.get();
	}

	/**
	 * 清除当前请求上下文
	 */
	public void end() {
		this.request = null;
		this.response = null;
		this.session = null;
		this.cookies = null;
		contexts.remove();
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public Map<String, Cookie> getCookies() {
		return cookies;
	}

	public void setCookies(Map<String, Cookie> cookies) {
		this.cookies = cookies;
	}

}
